<?php

namespace Xin\Robot;

use Xin\Capsule\Service;
use Xin\HttpClient\HasHttpClient;
use Xin\HttpClient\Response;
use Xin\Robot\Contracts\Robot;

abstract class AbstractRobot extends Service implements Robot
{
	use HasHttpClient;

	/**
	 * @var string
	 */
	protected $baseUrl;

	/**
	 * @var array
	 */
	protected $lastInfo;

	/**
	 * @return array
	 */
	public function getLastInfo()
	{
		return $this->lastInfo;
	}

	/**
	 * @param mixed $lastInfo
	 * @return void
	 */
	protected function setLastInfo($lastInfo)
	{
		$this->lastInfo = $lastInfo;
	}

	/**
	 * @inerhitDoc
	 */
	public function httpClientOptions()
	{
		return [
			'base_uri' => $this->baseUrl,
			'timeout' => 10,
			'cookies' => false,
			'headers' => [
				'Accept' => 'application/json',
				'Content-Type' => 'application/json',
				'X-Requested-With' => 'XMLHttpRequest',
			],
			'allow_redirects' => false,
			'http_errors' => false,
		];
	}

	/**
	 * @inerhitDoc
	 */
	protected function onHttpClientResponse(Response $response)
	{
		$this->setLastInfo($response->data());

		return $response;
	}
}
